require 'library/html_reporter.rb'
require 'library/xml_reporter.rb'
require 'library/methods.rb'
require 'rbconfig'
require 'watir'
require 'funfx'
require 'funfx/browser/firewatir'
require 'roo'
require 'trollop'
require 'builder'


begin
  opts = Trollop::options do
    version "0.1 Alpha (c) Robert H. Koertge Jr."
    banner <<-EOS
A watir/funfx based key-word drive framework.

Usage:
    ruby harness.rb [options]
where [options] are:
    EOS
    opt :file, "Test Case file to execute", :type => :string
  end
  Trollop::die :file, "must be specified" unless opts[:file_given]
  Trollop::die :file, "must exist" unless File.exist?(Dir.pwd + '/test/data/' + opts[:file])
end

begin
  $hostOS = Config::CONFIG['host_os']
  $testDataFile = opts[:file]
  $HTMLreport = HtmlReporter.new()
  $XMLreport  = XMLReporter.new()
  $test_report = $HTMLreport.createReport(Dir.pwd + '/test/reports/' + File.basename($testDataFile,".xls"))
  $test_XMLreport = $XMLreport.createReport(Dir.pwd + '/test/reports/' + File.basename($testDataFile,".xls"))
  @flex = ""
  
  #DEFINE  Test Case Columns in Spreadsheet
  $COMMAND = 2
  $EXPECTED_RESULT = 3
  $TEST_DATA = 4
  $PARAMETER1 = 5
  $PARAMETER2 = 6

  # Define Configuration Columns in Spreadsheet
  PROPERTY = 2
  VALUE = 3

  # Define Test Data Columns


  # Read Configuration Settings from Spreadsheet File
  case File.extname($testDataFile)
  when ".xls" 
    $ss = Excel.new(Dir.pwd + '/test/data/' + $testDataFile) #grab the data file in the same dirrectory
  when ".ods"
    $ss = Openoffice.new(Dir.pwd + '/test/data/' + $testDataFile) #grab the data file in the same dirrectory
  else
    error
  end
  
  $ss.default_sheet = "Configuration"
  
  $ss.first_row.upto($ss.last_row) do |$line|
    case $ss.cell($line,PROPERTY).strip
    when "Browser"
      case $ss.cell($line,VALUE).strip
      when "FireFox"
        Watir::Browser.default = 'firefox'
        $browser ||= Watir::Browser.new
      when "IE"
        Watir::Browser.default = 'ie'
        $browser ||= Watir::Browser.new
      when "Safari"
        Watir::Browser.default = 'safari'
        $browser ||= Watir::Browser.new
      when "Chrome"
        Watir::Browser.default = 'chrome'
        $browser ||= Watir::Browser.new
      else
        Watir::Browser.default = 'firefox'
        $browser ||= Watir::Browser.new
      end
      # when "VisibleBrower" bug in watir 1.6.2
    when "TestType"
      case $ss.cell($line,VALUE).strip
      when "Flex"
        @flex = $browser.flex_app('FlexObjectTest', 'FlexObjectTest') #TODO Make this come from configuration table
      when "HTML"
      end
    when "BrowserSpeed"
      case $ss.cell($line,VALUE).strip
      when "Fast"
      when "Zippy"
      when "Slow"
      end
    end
  end

  # Read TestData from Spreadsheet File
  $ss.default_sheet = "Example"

  $ss.first_row.upto($ss.last_row) do |$line|
    case $ss.cell($line,$COMMAND)

    when "Test Case", "Command"
    when "Info" then info
    when "SectionBreak" then $HTMLreport.addtoReport($test_report, "", 'HEADING', "", "")

    when "Goto"
      $browser.goto($ss.cell($line,$PARAMETER1))
      event

    when "Search"
      $browser.text_field(:name,'q').set($ss.cell($line,3))
      $browser.button(:value,/Search/i).click
      $browser.contains_text($ss.cell($line,$EXPECTED_RESULT)) ? pass : fail
  
    when "FxOpenTree"
      @tree = @flex.tree({:id => $ss.cell($line,$PARAMETER1)})
      @tree.open($ss.cell($line,$PARAMETER2))
      event

    when "FxSelectTree"
      @tree.select($ss.cell($line,$PARAMETER1))
      event

    else
      error
    end
  
    
  end

  $browser.close
  # Finish the report
  $HTMLreport.finishReport($test_report)
  $XMLreport.finishReport($test_XMLreport)
rescue
  # Send crash info to report
  $HTMLreport.addtoReport($test_report, 'Test crashed with reason '+$!, 'FAILED', 'Test crashed!', '&nbsp;')
  $XMLreport.addtoReport($test_XMLreport, 'Test crashed with reason '+$!, 'FAILED', 'Test crashed!', '&nbsp;')
 puts $!
  puts $!.backtrace
  # Finish the report
  $HTMLreport.finishReport($test_report)
  $XMLreport.finishReport($test_XMLreport)
  $browser.close
end